package org.footito.android.ui.views;

import java.util.ArrayList;
import java.util.TreeMap;

import org.footito.android.R;
import org.footito.android.data.Team;
import org.footito.android.data.TeamGroup;
import org.footito.android.data.database.FootitoDatabaseAdapter;
import org.footito.android.ui.activity.OthersActivity;
import org.footito.android.ui.activity.ProposerActivity;
import org.footito.android.ui.activity.ProposerFragment;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;

public class TeamSelectorDialog extends Dialog implements OnChildClickListener {

	private ExpandableListView list;
	private LayoutInflater inflater;
	private Context activityParente;

	public TeamSelectorDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		init();
	}

	public TeamSelectorDialog(Context context, int theme) {
		super(context, theme);
		init();
	}

	public TeamSelectorDialog(Context context) {
		super(context);
		activityParente = context;
		init();
	}

	private void init() {
		inflater = LayoutInflater.from(getContext());
		list = new ExpandableListView(getContext());
		list.setAdapter(new TeamSelectorListAdapter());
		list.setOnChildClickListener(this);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		setTitle(R.string.titleDialogSelectTeam);
		addContentView(list, params );
	}
	
	private class TeamSelectorListAdapter extends BaseExpandableListAdapter {

		private TeamGroup[] groups;
		private TreeMap<TeamGroup, ArrayList<Team>> teamsByGroups;

		public TeamSelectorListAdapter() {
			FootitoDatabaseAdapter db = FootitoDatabaseAdapter.getInstance(getContext());
			teamsByGroups = db.getAllTeamsByGroup();
			groups = (TeamGroup[])teamsByGroups.keySet().toArray(new TeamGroup[teamsByGroups.keySet().size()]);
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return teamsByGroups.get(groups[groupPosition]).get(childPosition);
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return teamsByGroups.get(groups[groupPosition]).get(childPosition).getId();
		}

		@Override
		public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
			LocalHolder holder;
			if (convertView == null){
				convertView = inflater.inflate(android.R.layout.simple_expandable_list_item_2, null);
				holder = new LocalHolder();
				convertView.setTag(holder);
				holder.tv = (TextView) convertView.findViewById(android.R.id.text1);
			} else {
				holder = (LocalHolder) convertView.getTag();
			}
			holder.tv.setText(((Team)getChild(groupPosition, childPosition)).getName());
			return convertView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return teamsByGroups.get(groups[groupPosition]).size();
		}

		@Override
		public Object getGroup(int groupPosition) {
			return groups[groupPosition];
		}

		@Override
		public int getGroupCount() {
			return groups.length;
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groups[groupPosition].getId();
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
			LocalHolder holder;
			if (convertView == null){
				convertView = inflater.inflate(android.R.layout.simple_expandable_list_item_1, null);
				holder = new LocalHolder();
				convertView.setTag(holder);
				holder.tv = (TextView) convertView.findViewById(android.R.id.text1);
			} else {
				holder = (LocalHolder) convertView.getTag();
			}
			holder.tv.setText("" + groups[groupPosition].getName());
			return convertView;
		}

		@Override
		public boolean hasStableIds() {
			return false;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}
		
		private class LocalHolder {
			public TextView tv;
		}
		
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
		if(activityParente instanceof ProposerActivity)
			((ProposerActivity)activityParente).getFragment().setTeam(id);
		if(activityParente instanceof OthersActivity) {
			((ProposerFragment)((OthersActivity)activityParente).getFragment()).setTeam(id);
		}
		this.dismiss();
		return false;
	}	

}
